import 'dart:io';

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:screenshot/screenshot.dart';

class BarChartWidget extends StatefulWidget {
  const BarChartWidget({super.key});

  @override
  State<BarChartWidget> createState() => _BarChartState();
}

class _BarChartState extends State<BarChartWidget> {

  ScreenshotController screenshotController = ScreenshotController();

  @override
  Widget build(BuildContext context) {
    return Screenshot(controller: screenshotController, child: GestureDetector(
      onTap: (){
        _captureAndSave();
      },
      child: Container(
        padding: const EdgeInsets.all(20),
        color: Colors.tealAccent,
        child: BarChart(
            BarChartData(
              alignment: BarChartAlignment.spaceBetween,
              titlesData:   FlTitlesData(
                leftTitles: AxisTitles(
                    sideTitles: SideTitles(showTitles: true,reservedSize: 30,interval: 1,getTitlesWidget:(value,meta){
                      return Text('${value.toInt()}',style: const TextStyle(
                          color: Colors.white,fontSize: 10,decoration: TextDecoration.none
                      ),);
                    })
                ),
                bottomTitles: AxisTitles(
                  sideTitles:SideTitles(showTitles: true,reservedSize: 30,interval: 1,getTitlesWidget:(value,meta){
                    return Text('${value.toInt()}',style: const TextStyle(
                        color: Colors.white,fontSize: 10,decoration: TextDecoration.none
                    ),);
                  }),
                ),
                rightTitles: const AxisTitles(
                  sideTitles: SideTitles(showTitles: false,interval: 1),
                ),
                topTitles: const AxisTitles(
                  sideTitles: SideTitles(showTitles: false,interval: 1),
                ),
              ),
              borderData: FlBorderData(show: true),
              barGroups: showingBarGroups(),
              maxY: 16,
            )
        ),
      ),

    ));
  }

  List<BarChartGroupData> showingBarGroups() {
    return [
      BarChartGroupData(
        x:0,
        barRods: [
          BarChartRodData(fromY:0, toY:8, color: Colors.blue,borderRadius:const BorderRadius.all(Radius.zero),width: 80),
        ],
      ),
      BarChartGroupData(
        x:1,
        barRods: [
          BarChartRodData(fromY:0, toY:6, color: Colors.red,borderRadius:const BorderRadius.all(Radius.zero),width: 80),
        ],
      ),
      BarChartGroupData(
        x:2,
        barRods: [
          BarChartRodData(fromY:0, toY:12, color: Colors.green,borderRadius:const BorderRadius.all(Radius.zero),width: 80),
        ],
      ),
      BarChartGroupData(
        x:3,
        barRods: [
          BarChartRodData(fromY:0, toY:10, color: Colors.orange,borderRadius:const BorderRadius.all(Radius.zero),width: 80),
        ],
      ),
      BarChartGroupData(
        x:4,
        barRods: [
          BarChartRodData(fromY:0, toY:15, color: Colors.purple,borderRadius:const BorderRadius.all(Radius.zero),width: 80),
        ],
      ),
    ];
  }

  void _captureAndSave() async {
    final image = await screenshotController.capture();

    if (image != null) {
      // 获取图片保存路径 final directory = await getApplicationDocumentsDirectory();
      const imagePath = 'D:\\screenshot.png';

      // 将图像写入文件
      final file = File(imagePath);
      await file.writeAsBytes(image);
    }
  }
}
